/*
 * @Author: liu_lizhi
 * @Date: 2021-01-08 10:51:02
 * @LastEditTime: 2021-07-28 19:00:30
 * @Description: 用户相关方法
 */

import { login, logout, uploadFile, downloadFile, downloadBigFile, getUserInfo, getMaxVersion} from '../../api/common'

const state = {
    token: '000',
    userId: '',
    name: '',
    loginName: '',
    teamList: [],
    roleList: [],
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_USERID: (state, userId) => {
        state.userId = userId
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_LOGIN_NAME: (state, loginName) => {
       state.loginName = loginName
   },
   SET_TEAM_LIST: (state, teamList) => {
       state.teamList = teamList
   },
   SET_ROLE_LIST: (state, roleList) => {
       state.roleList = roleList
   },
}

const actions = {
    //登录action
   login({commit}, params) {
       const { url, userInfo } = params
       return new Promise((resolve, reject) => {
           login(url, userInfo).then(res => {
               console.log('登录成功',res)
               if (res.success) {
                   let userInfo = res.data
                   commit('SET_TOKEN',userInfo.sessionId)
                   commit('SET_USERID',userInfo.userId)
                   commit('SET_NAME',userInfo.userName)
                   commit('SET_LOGIN_NAME', userInfo.loginName)
                   commit('SET_TEAM_LIST',[userInfo.orgDTO])                   
                   commit('SET_ROLE_LIST',[userInfo.roleDTO])
                   
                   let user = {
                       userId: userInfo.userId,
                       userName: userInfo.userName,
                       loginName: userInfo.loginName,
                       orgId: userInfo.orgDTO.orgId,
                       orgName: userInfo.orgDTO.orgName,
                       unitId: userInfo.orgDTO.unitId,
                       unitName: userInfo.orgDTO.unitName,
                       roleCode:userInfo.roleDTO.roleCode,
                       roleName:userInfo.roleDTO.roleName,
                       orgDTO: userInfo.orgDTO,
                       roleDTO: userInfo.roleDTO,
                   }

                   //供快捷获取用户信息使用
                   localStorage.userId = userInfo.userId
                   localStorage.unitId = user.unitId
                   localStorage.orgId = user.orgId
                   localStorage.orgName = user.orgName
                   localStorage.token = userInfo.sessionId
                   localStorage.loginName = userInfo.loginName

                   localStorage.setItem("user",JSON.stringify(user))
                   //因模块都是使用数组、如果修改为对象，改动太大
                   var obj = {
                       orgId: userInfo.orgDTO.orgId,
                       orgName: userInfo.orgDTO.orgName
                   }
                   var op = {
                       "roleCode": userInfo.roleDTO.roleCode,
                       "roleName": userInfo.roleDTO.roleName
                   }
                   localStorage.setItem("teamList", JSON.stringify([obj]))
                   localStorage.setItem("roleList", JSON.stringify([obj]))
                   resolve(res.data)
               } else {
                   reject()
               }
           }).catch(error => {
               reject(error)
           })
       }) 
   },
   // 退出
   Logout({ commit, state }) {
       return new Promise((resolve) => {
           let logoutToken = state.token;
           commit('SET_TOKEN', '')
           commit('SET_USERID','')
           commit('SET_NAME','')
           commit('SET_LOGIN_NAME', '')
           commit('SET_TEAM_LIST',[])
           commit('SET_ROLE_LIST', [])

           localStorage.userId = null
           localStorage.token = null
           localStorage.loginName = null
           localStorage.user = null
           logout('/logout',logoutToken).then(() => {
               resolve()
           }).catch(() => {
               resolve()
           })
       })
   },
   getUserInfo({commit},params) {
       const { url, data } = params
       return new Promise((resolve, reject) => {
           getUserInfo(url, data).then(res => {
               console.log('getUserInfo成功',res)
               if (res.success) {
                   resolve(res.data)
               } else {
                   reject(res.msg)
               }
           }).catch(error => {
               reject(error)
           })
       }) 
   },
   uploadFile({commit},params) {
       return new Promise((resolve, reject) => {
           // formdate 类型传参
           uploadFile(params).then(res => {
               if (res.success) {                   
                   resolve(res.data)
               } else {
                   reject(res.msg)
               }
           }).catch(error => {
               reject(error)
           })
       })
   },
   downloadFile({commit},params) {
       return new Promise((resolve, reject) => {
           // 未分片文件下载
           downloadFile(params).then(res => {
               if (res.success) {                   
                   resolve(res.data)
               } else {
                   reject(res.msg)
               }
           }).catch(error => {
               reject(error)
           })
       })
   },
   downloadBigFile({commit},params) {
       return new Promise((resolve, reject) => {
           // 分片文件下载
           downloadBigFile(params).then(() => {
               resolve()
           }).catch(error => {
               reject(error)
           })
       })
   },
   getMaxVersion({commit},params) {
       return new Promise((resolve, reject) => {
           //获取最新版本apk
           let url ='bdydzy-ptmo-pub/app/mobApp/v2/getMaxVersionInfo'
           getMaxVersion(url, params).then(res => {
               if (res.success) {                   
                   resolve(res.data)
               } else {
                   reject(res.msg)
               }
           }).catch(error => {
               reject(error)
           })
       })
   },

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
